/*
 * binary_search.c
 *
 *  Created on: 2011-12-14
 *      Author: root
 * description: 二分查找
 */
#include <stdio.h>

int binarySearch(int source[],int size,int key){
	int low=0;
	int mid=0;
	int high=size-1;
	while(low<=high){
		//mid=(low+high)/2;
		mid=low+(high-low)/2;
		if(key<source[mid]){
			high=mid-1;
		}else if(key>source[mid]){
			low=mid+1;
		}else{
			return mid;
		}
	}
	
	return -1;
}

void main_binary_search(){
	int x[]={1,2,3,4};
	int res=binarySearch(x,sizeof(x)/sizeof(int),3);
    printf("%d\n",res);
}
